// map/reduce/flat/mapflat/sum
// isArray/shuffle
// 去重/falt/排序

Array.prototype.myMap = function (cb) {
    let res = []
    for (let i = 0; i < this.length; i++) {
        res.push(cb.call(this, this[i], i, this))
    }
    return res;
}
console.log([1, 2, 3].map((item) => item + 1))
console.log([1, 2, 3].myMap((item) => item + 1))

Array.prototype.myReduce = function (cb, initval) {
    let res
    let startIndex;
    if (!initval) {
        startIndex = 1
        res = this[0]
    } else {
        startIndex = 0
        res = initval
    }
    for (let i = startIndex; i < this.length; i++) {
        res = cb.call(this, res, this[i], this)
    }
    return res;
}

console.log([1, 2, 3].reduce((pre, next) => pre + next, 3))
console.log([1, 2, 3].myReduce((pre, next) => pre + next, 3))

 
Array.prototype.myFlat = function (dep) {
    let res = []
    for (let i = 0; i < this.length; i++) {
        if (Array.isArray(this[i]) && dep > 0) {
            res = res.concat(this[i].myFlat(dep - 1))
        } else {
            res.push(this[i])
        }
    }
    return res;

}
console.log([1, [2, [3, 3]]].flat(2))
console.log([1, [2, [3, 3]]].myFlat(2))

Array.prototype.mySum = function () {
    return this.reduce((pre, next) => pre + next, 0)
}
console.log([1, 2, 3].mySum())


Array.isMyArrray = function (object) {
    return Object.prototype.toString.call(object) === '[object Array]'
}
console.log(Array.isMyArrray([]))

Array.prototype.isMyShuffle = function () {
    return this.sort((a, b) => Math.random() - 0.5)
}
console.log([1, 2, 3, 4, 5].isMyShuffle())

// 去重，排序
const setFlatSort = (arr) => {
   return Array.from(new Set(arr.flat(Infinity))).sort((a,b) => a - b)
}
console.log(setFlatSort([1,2,2,3,1,3]))


// 当元素个数小于 10 个的时候，使用插入排序；
// 当元素个数大于 10 个的时候，使用快速排序。
// 快排
const quickSort = (arr) => {
    if(!arr.length) return []
    let left = []
    let right = []
    let pivot = arr[0]
    for(let i = 1; i< arr.length; i++){
        if(arr[i] > pivot){
            right.push(arr[i])
        } else {
            left.push(arr[i])
        }
    }
    return quickSort(left).concat(pivot, quickSort(right))
}
console.log(quickSort([ 3, 4, 1, 5, 2 ]))

 
const insetSort = (arr) => {
    for(let i = 1; i< arr.length; i++){
        var temp = arr[i]
        var j = i - 1;
        while(j>= 0 && arr[j] > temp){
            arr[j+1] = arr[j]
            j--
        }
        arr[j+1] = key
        return arr
    }
}
console.log(quickSort([ 3, 4, 1, 5, 2 ]))

// 二维数组以row 分组
// 手写 fn([['a','b'],['0','1'],['m','n']]) => ['a0m', 'a0n'....]
// [a, b] * [0, 1] => [a0,a1, b0, b1]
// [a0,a1, b0, b1] * [m, n] => [a0m, a0n, ....]

// const transform = (arr) => {
//     let res = [];
//     arr.forEach(item => {
//         item.forEach((val, index) => {
//             res[index] = res[index] || [];
//             res[index].push(val)
//         })
//     })
//     return res;
// }
// console.log(transform([['a','b'],['0','1'],['m','n']]))

const transform = (arr) => {
    return arr.reduce((pre, next) => {
        let res = []
        pre.forEach(item => {
            next.forEach(val => {
                res.push(`${item}${val}`)
            })
        })
        return res;
    })
}
console.log(transform([['a','b'],['0','1'],['m','n']]))